package com.jmgd.cjlx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jmgd.cjlx.constants.MainConstant;
import com.jmgd.cjlx.dto.TXYVoiceDto;
import com.jmgd.cjlx.dto.TXYVoiceTelDto;
import com.jmgd.cjlx.service.TXYService;
import com.jmgd.cjlx.util.CommonUtil;
import com.jmgd.cjlx.util.HttpClientUtil;
import com.jmgd.cjlx.util.SHAUtil;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TXYServiceImpl implements TXYService {
    @Override
    public String sendVoice(String tel) {
        String random = CommonUtil.getRandomNickname(10);
        String strTime = Long.toString(System.currentTimeMillis()/1000L); //UNIX 时间戳
        String url = MainConstant.voice_url;
        url = url.replaceAll("SDKAPPID",MainConstant.sdkappid);
        url = url.replaceAll("RANDOM",random);
        TXYVoiceDto txyVoiceDto = new TXYVoiceDto();
        txyVoiceDto.setTpl_id(MainConstant.tpl_id);
        txyVoiceDto.setParams(new String[0]);
        txyVoiceDto.setPlaytimes(2);
        //String tel = "13422654164";
        String sigTemp = "appkey="+MainConstant.sdkappkey+"&random="+random+"&time="+strTime+"&mobile="+tel;
        String sig = SHAUtil.SHA256(sigTemp);
        txyVoiceDto.setSig(sig);
        TXYVoiceTelDto txyVoiceTelDto = new TXYVoiceTelDto();
        txyVoiceTelDto.setMobile(tel);
        txyVoiceTelDto.setNationcode("86");

        txyVoiceDto.setTel(txyVoiceTelDto);
        txyVoiceDto.setTime(Integer.parseInt(strTime));
        txyVoiceDto.setExt("");
        JSONObject json=(JSONObject) JSONObject.toJSON(txyVoiceDto);
        Map map = HttpClientUtil.doPost(url,null,json);
        return "success";
    }
}
